home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / language / japan-util.el.z / japan-util.el
Encoding:
Text File  |  1998-05-21  |  13.8 KB  |  310 lines

  1. ;;; japan-util.el ---  utilities for Japanese
  2.  
  3. ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
  4. ;; Licensed to the Free Software Foundation.
  5. ;; Copyright (C) 1997 MORIOKA Tomohiko
  6.  
  7. ;; Keywords: mule, multilingual, Japanese
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Code:
  27.  
  28. ;;;###autoload
  29. (defun setup-japanese-environment ()
  30.   "Setup multilingual environment (MULE) for Japanese."
  31.   (interactive)
  32.   (setup-english-environment)
  33.  
  34.   ;; (setq coding-category-iso-8-2 'japanese-iso-8bit)
  35.   (set-coding-category-system 'iso-8-2 'euc-jp)
  36.   ;; (setq coding-category-iso-8-else 'japanese-iso-8bit)
  37.   
  38.   ;; (set-coding-priority
  39.   ;;  '(coding-category-iso-7
  40.   ;;    coding-category-iso-8-2
  41.   ;;    coding-category-sjis
  42.   ;;    coding-category-iso-8-1
  43.   ;;    coding-category-iso-7-else
  44.   ;;    coding-category-iso-8-else
  45.   ;;    coding-category-emacs-mule))
  46.   (set-coding-priority-list
  47.    '(iso-7
  48.      iso-8-2
  49.      shift-jis
  50.      iso-8-1
  51.      iso-lock-shift
  52.      iso-8-designate
  53.      no-conversion
  54.      big5))
  55.   
  56.   (set-default-coding-systems
  57.    (if (eq system-type 'ms-dos)
  58.        'japanese-shift-jis
  59.      'iso-2022-jp))
  60.   
  61.   ;; (when (eq 'x (device-type (selected-device)))
  62.   ;;   (x-use-halfwidth-roman-font 'japanese-jisx0208 "jisx0201"))
  63.   
  64.   ;; (setq default-input-method "japanese")
  65.   )
  66.  
  67. (defconst japanese-kana-table
  68.   '((?$B$"(B ?$B%"(B ?(I1(B) (?$B$$(B ?$B%$(B ?(I2(B) (?$B$&(B ?$B%&(B ?(I3(B) (?$B$((B ?$B%((B ?(I4(B) (?$B$*(B ?$B%*(B ?(I5(B)
  69.     (?$B$+(B ?$B%+(B ?(I6(B) (?$B$-(B ?$B%-(B ?(I7(B) (?$B$/(B ?$B%/(B ?(I8(B) (?$B$1(B ?$B%1(B ?(I9(B) (?$B$3(B ?$B%3(B ?(I:(B)
  70.     (?$B$5(B ?$B%5(B ?(I;(B) (?$B$7(B ?$B%7(B ?(I<(B) (?$B$9(B ?$B%9(B ?(I=(B) (?$B$;(B ?$B%;(B ?(I>(B) (?$B$=(B ?$B%=(B ?(I?(B)
  71.     (?$B$?(B ?$B%?(B ?(I@(B) (?$B$A(B ?$B%A(B ?(IA(B) (?$B$D(B ?$B%D(B ?(IB(B) (?$B$F(B ?$B%F(B ?(IC(B) (?$B$H(B ?$B%H(B ?(ID(B)
  72.     (?$B$J(B ?$B%J(B ?(IE(B) (?$B$K(B ?$B%K(B ?(IF(B) (?$B$L(B ?$B%L(B ?(IG(B) (?$B$M(B ?$B%M(B ?(IH(B) (?$B$N(B ?$B%N(B ?(II(B)
  73.     (?$B$O(B ?$B%O(B ?(IJ(B) (?$B$R(B ?$B%R(B ?(IK(B) (?$B$U(B ?$B%U(B ?(IL(B) (?$B$X(B ?$B%X(B ?(IM(B) (?$B$[(B ?$B%[(B ?(IN(B)
  74.     (?$B$^(B ?$B%^(B ?(IO(B) (?$B$_(B ?$B%_(B ?(IP(B) (?$B$`(B ?$B%`(B ?(IQ(B) (?$B$a(B ?$B%a(B ?(IR(B) (?$B$b(B ?$B%b(B ?(IS(B)
  75.     (?$B$d(B ?$B%d(B ?(IT(B) (?$B$f(B ?$B%f(B ?(IU(B) (?$B$h(B ?$B%h(B ?(IV(B)
  76.     (?$B$i(B ?$B%i(B ?(IW(B) (?$B$j(B ?$B%j(B ?(IX(B) (?$B$k(B ?$B%k(B ?(IY(B) (?$B$l(B ?$B%l(B ?(IZ(B) (?$B$m(B ?$B%m(B ?(I[(B)
  77.     (?$B$o(B ?$B%o(B ?(I\(B) (?$B$p(B ?$B%p(B nil) (?$B$q(B ?$B%q(B nil) (?$B$r(B ?$B%r(B ?(I&(B)
  78.     (?$B$s(B ?$B%s(B ?(I](B)
  79.     (?$B$,(B ?$B%,(B "(I6^(B") (?$B$.(B ?$B%.(B "(I7^(B") (?$B$0(B ?$B%0(B "(I8^(B") (?$B$2(B ?$B%2(B "(I9^(B") (?$B$4(B ?$B%4(B "(I:^(B")
  80.     (?$B$6(B ?$B%6(B "(I;^(B") (?$B$8(B ?$B%8(B "(I<^(B") (?$B$:(B ?$B%:(B "(I=^(B") (?$B$<(B ?$B%<(B "(I>^(B") (?$B$>(B ?$B%>(B "(I?^(B")
  81.     (?$B$@(B ?$B%@(B "(I@^(B") (?$B$B(B ?$B%B(B "(IA^(B") (?$B$E(B ?$B%E(B "(IB^(B") (?$B$G(B ?$B%G(B "(IC^(B") (?$B$I(B ?$B%I(B "(ID^(B")
  82.     (?$B$P(B ?$B%P(B "(IJ^(B") (?$B$S(B ?$B%S(B "(IK^(B") (?$B$V(B ?$B%V(B "(IL^(B") (?$B$Y(B ?$B%Y(B "(IM^(B") (?$B$\(B ?$B%\(B "(IN^(B")
  83.     (?$B$Q(B ?$B%Q(B "(IJ_(B") (?$B$T(B ?$B%T(B "(IK_(B") (?$B$W(B ?$B%W(B "(IL_(B") (?$B$Z(B ?$B%Z(B "(IM_(B") (?$B$](B ?$B%](B "(IN_(B")
  84.     (?$B$!(B ?$B%!(B ?(I'(B) (?$B$#(B ?$B%#(B ?(I((B) (?$B$%(B ?$B%%(B ?(I)(B) (?$B$'(B ?$B%'(B ?(I*(B) (?$B$)(B ?$B%)(B ?(I+(B)
  85.     (?$B$C(B ?$B%C(B ?(I/(B)
  86.     (?$B$c(B ?$B%c(B ?(I,(B) (?$B$e(B ?$B%e(B ?(I-(B) (?$B$g(B ?$B%g(B ?(I.(B)
  87.     (?$B$n(B ?$B%n(B nil)
  88.     (nil ?$B%t(B "(I3^(B") (nil ?$B%u(B nil) (nil ?$B%v(B nil))
  89.   "Japanese JISX0208 Kana character table.
  90. Each element is of the form (HIRAGANA KATAKANA HANKAKU-KATAKANA), where
  91. HIRAGANA and KATAKANA belong to `japanese-jisx0208',
  92. HANKAKU-KATAKANA belongs to `japanese-jisx0201-kana'.")
  93.  
  94. ;; Put properties 'katakana, 'hiragana, and 'jix0201 to each Japanese
  95. ;; kana characters for conversion among them.
  96. (let ((l japanese-kana-table)
  97.       slot hiragana katakana jisx0201)
  98.   (while l
  99.     (setq slot (car l)
  100.       hiragana (car slot) katakana (nth 1 slot) jisx0201 (nth 2 slot)
  101.       l (cdr l))
  102.     (if hiragana
  103.     (progn
  104.       (put-char-code-property hiragana 'katakana katakana)
  105.       (put-char-code-property katakana 'hiragana hiragana)
  106.       (if jisx0201
  107.           (progn
  108.         (put-char-code-property hiragana 'jisx0201 jisx0201)
  109.         (if (integerp jisx0201)
  110.             (put-char-code-property jisx0201 'hiragana hiragana))))))
  111.     (if jisx0201
  112.     (progn
  113.       (put-char-code-property katakana 'jisx0201 jisx0201)
  114.       (if (integerp jisx0201)
  115.           (put-char-code-property jisx0201 'katakana katakana))))))
  116.  
  117. (defconst japanese-symbol-table
  118.   '((?\$B!!(B ?\ ) (?$B!"(B ?, ?(I$(B) (?$B!#(B ?. ?(I!(B) (?$B!$(B ?, ?(I$(B) (?$B!%(B ?. ?(I!(B) (?$B!&(B nil ?(I%(B)
  119.     (?$B!'(B ?:) (?$B!((B ?\;) (?$B!)(B ??) (?$B!*(B ?!) (?$B!+(B nil ?(I^(B) (?$B!,(B nil ?(I_(B)
  120.     (?$B!-(B ?') (?$B!.(B ?`) (?$B!0(B ?^) (?$B!2(B ?_) (?$B!<(B ?-) (?$B!=(B ?-) (?$B!>(B ?-)
  121.     (?$B!?(B ?/) (?$B!@(B ?\\) (?$B!A(B ?~)  (?$B!C(B ?|) (?$B!F(B ?`) (?$B!G(B ?') (?$B!H(B ?\") (?$B!I(B ?\")
  122.     (?\$B!J(B ?\() (?\$B!K(B ?\)) (?\$B!N(B ?[) (?\$B!O(B ?]) (?\$B!P(B ?{) (?\$B!Q(B ?})
  123.     (?$B!R(B ?<) (?$B!S(B ?>) (?$B!\(B ?+) (?$B!](B ?-) (?$B!a(B ?=) (?$B!c(B ?<) (?$B!d(B ?>)
  124.     (?$B!l(B ?') (?$B!m(B ?\") (?$B!o(B ?\\) (?$B!p(B ?$) (?$B!s(B ?%) (?$B!t(B ?#) (?$B!u(B ?&) (?$B!v(B ?*)
  125.     (?$B!w(B ?@))
  126.   "Japanese JISX0208 symbol character table.
  127.   Each element is of the form (SYMBOL ASCII HANKAKU), where SYMBOL
  128. belongs to `japanese-jisx0208', ASCII belongs to `ascii', and HANKAKU
  129. belongs to `japanese-jisx0201-kana'.")
  130.  
  131. ;; Put properties 'jisx0208, 'jisx0201, and 'ascii to each Japanese
  132. ;; symbol and ASCII characters for conversion among them.
  133. (let ((l japanese-symbol-table)
  134.       slot jisx0208 ascii jisx0201)
  135.   (while l
  136.     (setq slot (car l)
  137.       jisx0208 (car slot) ascii (nth 1 slot) jisx0201 (nth 2 slot)
  138.       l (cdr l))
  139.     (if ascii
  140.     (progn
  141.       (put-char-code-property jisx0208 'ascii ascii)
  142.       (put-char-code-property ascii 'jisx0208 jisx0208)))
  143.     (if jisx0201
  144.     (progn
  145.       (put-char-code-property jisx0208 'jisx0201 jisx0201)
  146.       (put-char-code-property jisx0201 'jisx0208 jisx0208)))))
  147.  
  148. (defconst japanese-alpha-numeric-table
  149.   '((?$B#0(B . ?0) (?$B#1(B . ?1) (?$B#2(B . ?2) (?$B#3(B . ?3) (?$B#4(B . ?4)
  150.     (?$B#5(B . ?5) (?$B#6(B . ?6) (?$B#7(B . ?7) (?$B#8(B . ?8) (?$B#9(B . ?9)
  151.     (?$B#A(B . ?A) (?$B#B(B . ?B) (?$B#C(B . ?C) (?$B#D(B . ?D) (?$B#E(B . ?E) 
  152.     (?$B#F(B . ?F) (?$B#G(B . ?G) (?$B#H(B . ?H) (?$B#I(B . ?I) (?$B#J(B . ?J)
  153.     (?$B#K(B . ?K) (?$B#L(B . ?L) (?$B#M(B . ?M) (?$B#N(B . ?N) (?$B#O(B . ?O)
  154.     (?$B#P(B . ?P) (?$B#Q(B . ?Q) (?$B#R(B . ?R) (?$B#S(B . ?S) (?$B#T(B . ?T)
  155.     (?$B#U(B . ?U) (?$B#V(B . ?V) (?$B#W(B . ?W) (?$B#X(B . ?X) (?$B#Y(B . ?Y) (?$B#Z(B . ?Z)
  156.     (?$B#a(B . ?a) (?$B#b(B . ?b) (?$B#c(B . ?c) (?$B#d(B . ?d) (?$B#e(B . ?e)
  157.     (?$B#f(B . ?f) (?$B#g(B . ?g) (?$B#h(B . ?h) (?$B#i(B . ?i) (?$B#j(B . ?j)
  158.     (?$B#k(B . ?k) (?$B#l(B . ?l) (?$B#m(B . ?m) (?$B#n(B . ?n) (?$B#o(B . ?o)
  159.     (?$B#p(B . ?p) (?$B#q(B . ?q) (?$B#r(B . ?r) (?$B#s(B . ?s) (?$B#t(B . ?t)
  160.     (?$B#u(B . ?u) (?$B#v(B . ?v) (?$B#w(B . ?w) (?$B#x(B . ?x) (?$B#y(B . ?y) (?$B#z(B . ?z))
  161.   "Japanese JISX0208 alpha numeric character table.
  162. Each element is of the form (ALPHA-NUMERIC ASCII), where ALPHA-NUMERIC
  163. belongs to `japanese-jisx0208', ASCII belongs to `ascii'.")
  164.  
  165. ;; Put properties 'jisx0208 and 'ascii to each Japanese alpha numeric
  166. ;; and ASCII characters for conversion between them.
  167. (let ((l japanese-alpha-numeric-table)
  168.       slot jisx0208 ascii)
  169.   (while l
  170.     (setq slot (car l)
  171.       jisx0208 (car slot) ascii (cdr slot)
  172.       l (cdr l))
  173.     (put-char-code-property jisx0208 'ascii ascii)
  174.     (put-char-code-property ascii 'jisx0208 jisx0208)))
  175.  
  176. ;; Convert string STR by FUNC and return a resulting string.
  177. (defun japanese-string-conversion (str func &rest args)
  178.   (let ((buf (get-buffer-create " *Japanese work*")))
  179.     (save-excursion
  180.       (set-buffer buf)
  181.       (erase-buffer)
  182.       (insert str)
  183.       (apply func 1 (point) args)
  184.       (buffer-string))))
  185.  
  186. ;;;###autoload
  187. (defun japanese-katakana (obj &optional hankaku)
  188.   "Convert argument to Katakana and return that.
  189. The argument may be a character or string.  The result has the same type.
  190. The argument object is not altered--the value is a copy.
  191. Optional argument HANKAKU t means to convert to `hankaku' Katakana
  192.  \(`japanese-jisx0201-kana'), in which case return value
  193.  may be a string even if OBJ is a character if two Katakanas are
  194.  necessary to represent OBJ."
  195.   (if (stringp obj)
  196.       (japanese-string-conversion obj 'japanese-katakana-region hankaku)
  197.     (or (get-char-code-property obj (if hankaku 'jisx0201 'katakana))
  198.     obj)))
  199.  
  200. ;;;###autoload
  201. (defun japanese-hiragana (obj)
  202.   "Convert argument to Hiragana and return that.
  203. The argument may be a character or string.  The result has the same type.
  204. The argument object is not altered--the value is a copy."
  205.   (if (stringp obj)
  206.       (japanese-string-conversion obj 'japanese-hiragana-region)
  207.     (or (get-char-code-property obj 'hiragana)
  208.     obj)))
  209.  
  210. ;;;###autoload
  211. (defun japanese-hankaku (obj &optional ascii-only)
  212.   "Convert argument to `hankaku' and return that.
  213. The argument may be a character or string.  The result has the same type.
  214. The argument object is not altered--the value is a copy.
  215. Optional argument ASCII-ONLY non-nil means to return only ASCII character."
  216.   (if (stringp obj)
  217.       (japanese-string-conversion obj 'japanese-hankaku-region ascii-only)
  218.     (or (get-char-code-property obj 'ascii)
  219.     (and (not ascii-only)
  220.          (get-char-code-property obj 'jisx0201))
  221.     obj)))
  222.  
  223. ;;;###autoload
  224. (defun japanese-zenkaku (obj)
  225.   "Convert argument to `zenkaku' and return that.
  226. The argument may be a character or string.  The result has the same type.
  227. The argument object is not altered--the value is a copy."
  228.   (if (stringp obj)
  229.       (japanese-string-conversion obj 'japanese-zenkaku-region)
  230.     (or (get-char-code-property obj 'jisx0208)
  231.     obj)))
  232.  
  233. ;;;###autoload
  234. (defun japanese-katakana-region (from to &optional hankaku)
  235.   "Convert Japanese `hiragana' chars in the region to `katakana' chars.
  236. Optional argument HANKAKU t means to convert to `hankaku katakana' character
  237. of which charset is `japanese-jisx0201-kana'."
  238.   (interactive "r\nP")
  239.   (save-restriction
  240.     (narrow-to-region from to)
  241.     (goto-char (point-min))
  242.     (while (re-search-forward "\\cH\\|\\cK" nil t)
  243.       (let* ((hira (preceding-char))
  244.          (kata (japanese-katakana hira hankaku)))
  245.     (if kata
  246.         (progn
  247.           (delete-region (match-beginning 0) (match-end 0))
  248.           (insert kata)))))))
  249.  
  250. ;;;###autoload
  251. (defun japanese-hiragana-region (from to)
  252.   "Convert Japanese `katakana' chars in the region to `hiragana'  chars."
  253.   (interactive "r")
  254.   (save-restriction
  255.     (narrow-to-region from to)
  256.     (goto-char (point-min))
  257.     (while (re-search-forward "\\cK\\|\\ck" nil t)
  258.       (let* ((kata (preceding-char))
  259.          (hira (japanese-hiragana kata)))
  260.     (if hira
  261.         (progn
  262.           (delete-region (match-beginning 0) (match-end 0))
  263.           (insert hira)))))))
  264.  
  265. ;;;###autoload
  266. (defun japanese-hankaku-region (from to &optional ascii-only)
  267.   "Convert Japanese `zenkaku' chars in the region to `hankaku' chars.
  268. `Zenkaku' chars belong to `japanese-jisx0208'
  269. `Hankaku' chars belong to `ascii' or `japanese-jisx0201-kana'.
  270. Optional argument ASCII-ONLY non-nil means to convert only to ASCII char."
  271.   (interactive "r\nP")
  272.   (save-restriction
  273.     (narrow-to-region from to)
  274.     (goto-char (point-min))
  275.     (while (re-search-forward "\\cj" nil t)
  276.       (let* ((zenkaku (preceding-char))
  277.          (hankaku (japanese-hankaku zenkaku ascii-only)))
  278.     (if hankaku
  279.         (progn
  280.           (delete-region (match-beginning 0) (match-end 0))
  281.           (insert hankaku)))))))
  282.  
  283. ;;;###autoload
  284. (defun japanese-zenkaku-region (from to)
  285.   "Convert hankaku' chars in the region to Japanese `zenkaku' chars.
  286. `Zenkaku' chars belong to `japanese-jisx0208'
  287. `Hankaku' chars belong to `ascii' or `japanese-jisx0201-kana'."
  288.   (interactive "r")
  289.   (save-restriction
  290.     (narrow-to-region from to)
  291.     (goto-char (point-min))
  292.     (while (re-search-forward "\\ca\\|\\ck" nil t)
  293.       (let* ((hankaku (preceding-char))
  294.          (zenkaku (japanese-zenkaku hankaku)))
  295.     (if zenkaku
  296.         (progn
  297.           (delete-region (match-beginning 0) (match-end 0))
  298.           (insert zenkaku)))))))
  299.  
  300. ;;;###autoload
  301. (defun read-hiragana-string (prompt &optional initial-input)
  302.   "Read a Hiragana string from the minibuffer, prompting with string PROMPT.
  303. If non-nil, second arg INITIAL-INPUT is a string to insert before reading."
  304.   (read-multilingual-string prompt initial-input "japanese-hiragana"))
  305.  
  306. ;;
  307. (provide 'japan-util)
  308.  
  309. ;;; japan-util.el ends here
  310.